﻿using Volo.Abp.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Authorization;
using Itech_Run.Sys.Application.Contracts.Dto;
using Itech_Run.Sys.Application.Contracts.Service;
using Itech_Run.HttpApi.Shared;
using Microsoft.Extensions.Options;
using Itech_Run.Helper.Configure;
using Itech_Run.Helper.Cryptography;
using Volo.Abp.Application.Dtos;
using Itech_Run.HttpApi.Shared.Result;
using Itech_Run.Helper.Attribute;
using Itech_Run.HttpApi.Shared.Options;

namespace Itech_Run.Sys.HttpApi.Controllers
{
    public class SysUserController : BaseApiController
    {
        private readonly ISysUserService _sysUserService;
        private readonly IOptions<AesOptions> _aesOptions;
        private readonly IOptions<OtherOptions> _otherOptions;
        public SysUserController(ISysUserService sysUserService, IOptions<AesOptions> aesOptions, IOptions<OtherOptions> otherOptions)
        {
            _sysUserService = sysUserService;
            _aesOptions = aesOptions;
            _otherOptions = otherOptions;
        }

        [HttpGet]
        public async Task<CustomResult> GetAsync(string id)
        {
            return new CustomResult(await _sysUserService.GetAsync(id));
        }

        [HttpGet]
        public async Task<CustomResult> GetUserCurrentAsync()
        {
            return new CustomResult(await _sysUserService.GetAsync(User.Identity.Name));
        }

        [ButtonAuth(ButtonAuthConst.delete)]
        [HttpPost]
        public async Task<CustomResult> DeleteAsync(string id)
        {
            await _sysUserService.DeleteAsync(id);
            return GetSuccesResult();
        }

        [HttpGet]
        public async Task<CustomResult> GetListKeyValueAsync()
        {
            return new CustomResult(await _sysUserService.GetListAsync());
        }

        [HttpPost]
        public async Task<CustomResult> GetListAsync(SysUserListDto listDto)
        {
            return new CustomResult(await _sysUserService.GetListAsync(listDto));
        }

        [ButtonAuth(ButtonAuthConst.add)]
        [HttpPost]
        public async Task<CustomResult> InsertAsync(SysUserCreateDto dto)
        {
            dto.Password = dto.Password.AesEncypt(_aesOptions.Value.Key, _aesOptions.Value.IV);
            string operateUser = GetOperateId();
            return new CustomResult(await _sysUserService.InsertAsync(dto, operateUser));
        }

        [ButtonAuth(ButtonAuthConst.edit)]
        [HttpPost]
        public async Task<CustomResult> UpdateAsync(SysUserUpdateDto dto)
        {
            string operateUser = GetOperateId();
            return new CustomResult(await _sysUserService.UpdateAsync(dto, operateUser));
        }

        [HttpPost]
        public async Task<CustomResult> ChangePasswordForce(string id, string password)
        {
            password = password.AesEncypt(_aesOptions.Value.Key, _aesOptions.Value.IV);
            await _sysUserService.ChangePasswordForce(id, password, GetOperateId());
            return GetSuccesResult();
        }

        [HttpPost]
        public async Task<CustomResult> ChangePassword(string userName, string oldPassword, string newPassword)
        {
            oldPassword = oldPassword.AesEncypt(_aesOptions.Value.Key, _aesOptions.Value.IV);
            newPassword = newPassword.AesEncypt(_aesOptions.Value.Key, _aesOptions.Value.IV);
            await _sysUserService.ChangePassword(userName, oldPassword, newPassword, GetOperateId());
            return GetSuccesResult();
        }

        [AllowAnonymous]
        [HttpGet]
        public async Task<CustomResult> GetResumeAsync()
        {
            string operateUser = GetOperateId();
            if (operateUser == "")
            {
                operateUser = _otherOptions.Value.DefaultUserId;
            }
            return new CustomResult(await _sysUserService.GetResume(operateUser) ?? "");
        }
    }
}
